//app.js
import http from './utils/http.js'
App({
  onLaunch: function () {
    let me =this;
    me.checkSession();
  },
  checkSession(){
    let me=this;
    wx.checkSession({
      success: function () {
        console.log("未过期");
        let userId = wx.getStorageSync("userId");
        let sessionkey = wx.getStorageSync("session_key");
        if(!sessionkey){
          wx.removeStorageSync("userId");
        }
        if (!userId || !sessionkey){
          me.wxLogin();
        }
      },
      fail: function () {
        // session_key 已过期
        me.wxLogin();
      }
    })
  },

  wxLogin(){
    let me = this;
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        me.get3rdSeesion(res.code);
      }
    })
  },
  get3rdSeesion(code){
    let me = this;
    http.request({
      url: 'user/get3rdSeesion/'+code,
      method: 'post',
      success: res => {
        wx.setStorageSync("session_key", res);
      }
    });
  },
  getSessionKey(){
    return wx.getStorageSync('session_key');
  },
  isBind(){
    let userId = wx.getStorageSync('userId');
    let phone = wx.getStorageSync('phone');
    if (userId && phone){
      return true;
    }else{
      return false;
    }
  },
  globalData: {
    imgDomain:'http://image.fuyj.com.cn/',
    userInfo: null
  }
})